package com.dodoke.controller;

import java.util.ArrayList;
import java.util.List;

import com.dodoke.common.annotation.Log;
import com.dodoke.common.core.page.TableDataInfo;
import com.dodoke.common.enums.BusinessType;
import com.dodoke.common.utils.RandomUtil;
import com.dodoke.common.utils.poi.ExcelUtil;
import com.dodoke.domain.WxGoods;
import com.dodoke.domain.vo.WxGoodsVo;
import com.dodoke.service.IWxDetailQrService;
import com.dodoke.service.IWxGoodsService;
import com.dodoke.validateGroup.OnAdd;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.dodoke.common.core.controller.BaseController;
import com.dodoke.common.core.domain.AjaxResult;

import javax.validation.constraints.NotNull;

/**
 * 【请填写功能名称】Controller
 *
 * @author dodoke
 * @date 2020-08-25
 */
@RequestMapping("/goods")
@Api("微信商品管理")
@RestController
@Validated
public class WxGoodsController extends BaseController {

    @Autowired
    private IWxGoodsService wxGoodsService;

    @Autowired
    private IWxDetailQrService wxDetailQrService;
    // 转盘上的的商品数量
    private static final int DIAL_GOODS_COUNT = 6;

    /**
     * 根据二维码随机抽6个商品到转盘上
     * @param groupNum 前端传过来的组号
     */
    @ApiOperation("随机抽取6个商品到转盘上")
    @ApiImplicitParam(name = "groupNum", value = "二维码编码号", paramType = "path",required = true)
    @ApiResponses({
            @ApiResponse(code = 200, message = "获取商品列表成功"),
            @ApiResponse(code = 400, message = "二维码参数错误")
        }
    )
    @RequestMapping(value = "/list/{groupNum}",method = RequestMethod.GET)
    public AjaxResult list(@PathVariable("groupNum") Long groupNum) {
        List<Integer> ids = wxDetailQrService.selectQrGoodsIds(groupNum);
        List<Integer> noRepeatRamdom = RandomUtil.getNoRepeatRamdom(DIAL_GOODS_COUNT,ids.size());
        ArrayList<Integer> integers = new ArrayList<>(10);
        noRepeatRamdom.forEach(e->{
            integers.add(ids.get(e));
        });
        List<WxGoodsVo> wxGoodsVos = wxGoodsService.selectWxGoodsByRowNums(integers);
        return AjaxResult.success(wxGoodsVos);

    }

    /***
     * 根据商品name查看商品信息
     * @param name 商品name
     * @return 商品信息
     */
    @ApiOperation("查看商品的信息")
    @ApiImplicitParam(name = "name",value = "商品name",required = true,paramType = "path")
    @RequestMapping("/selectWxGoodsByName")
    public AjaxResult selectWxGoodsById(@RequestParam("goodsName") String name) {
        WxGoods wxGoods = wxGoodsService.selectWxGoodsByName(name);
        return AjaxResult.success(wxGoods);
    }

    /***
     * 查看商品有无库存
     * @param id 商品id
     * @return 布尔值，有或者无
     */
    @ApiOperation("查看商品有无库存")
    @ApiImplicitParam(name = "id",value = "商品id",required = true,paramType = "path")
    @RequestMapping("/selectGoodsCount/{id}")
    public AjaxResult selectGoodsCount(@PathVariable(value = "id") Long id) {
        int count = wxGoodsService.selectGoodsCount(id);
        boolean flag = count != 0;
        return AjaxResult.success(flag);
    }

    /***
     * 根据商品id查看商品信息
     * @param id 商品id
     * @return 商品信息
     */
    @ApiOperation("查看商品的信息")
    @ApiImplicitParam(name = "id",value = "商品id",required = true,paramType = "path")
    @RequestMapping("/selectWxGoodsById/{id}")
    public AjaxResult selectWxGoodsById(@PathVariable(value = "id") Long id) {
        WxGoods wxGoods = wxGoodsService.selectWxGoodsById(id);
        return AjaxResult.success(wxGoods);
    }


    /***
     * 通过商品id查询【商品】
     * 和上面有所区分，这是给后端人员返回的数据，多带了‘成本’、‘概率’两项数据
     * @param id
     * @return
     */
    @ApiOperation("查看商品的信息")
    @ApiImplicitParam(name = "id",value = "商品id",required = true,paramType = "path")
    @RequestMapping("/selectWxGoodsById4Vue/{id}")
    public AjaxResult selectWxGoodsById4Vue(@PathVariable(value = "id") Long id) {
        WxGoods wxGoods = wxGoodsService.selectWxGoodsById4Vue(id);
        return AjaxResult.success(wxGoods);
    }


    /***
     * 新增商品，商品是商品，图片是图片，分开来添加
     * @param wxGoods 商品对应的实体类
     * @return 影响行数对应的AjaxResult
     */
    @ApiOperation("新增商品")
    @ApiImplicitParam(name = "wxGoods",value = "商品对应的实体类",dataType = "WxGoods",required = true,paramType = "form")
    @RequestMapping(value = "/addGoods",method = RequestMethod.POST)
    @Validated(value = {OnAdd.class})
    public AjaxResult addGoods(@RequestBody WxGoods wxGoods) {
        int affectNum = wxGoodsService.addGoods(wxGoods);
        return toAjax(affectNum);
    }


    /***
     * 获取所有的商品,需要联查wx_image获取商品对应的image
     * 还需要联查wx_goods_attr获取商品对应的参数
     * @return
     */
    @ApiOperation("获取所有的商品")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "wxGoods",value = "商品对应的实体类",dataType = "WxGoods",required = true,paramType = "form"),
            @ApiImplicitParam(name = "pageNum",value = "当前页码",dataType = "int"),
            @ApiImplicitParam(name = "pageSize",value = "每页展示的记录条数",dataType = "int"),
            @ApiImplicitParam(name = "orderByColumn",value = "由哪个字段排序",dataType = "String"),
            @ApiImplicitParam(name = "isAsc",value = "升序降序",dataType = "String")
    })
    @RequestMapping(value = "/findAllGoods",method = RequestMethod.GET)
    public TableDataInfo findAllGoods(WxGoods wxGoods) {
        startPage();
        List<WxGoods> allGoods = wxGoodsService.findAllGoods(wxGoods);
        return getDataTable(allGoods);
    }

    /***
     * 查找不在该小程序码对应的商品池的那些商品，给后台页面使用
     * @param id
     * @return
     */
    @ApiOperation("查找不在该小程序码对应的商品池的那些商品")
    @ApiImplicitParam(name = "id",value = "当前小程序码的id",dataType = "int",paramType = "path",required = true)
    @GetMapping("/findAllGoodsNotInQr/{id}")
    public AjaxResult findAllGoodsNotInQr(@PathVariable Long id) {
        return AjaxResult.success(wxGoodsService.findAllGoodsNotInQr(id));
    }

    /***
     * 查找该小程序码对应的商品池的那些商品，给后台页面使用
     * @param id
     * @return
     */
    @ApiOperation("查找该小程序码对应的商品池的那些商品")
    @ApiImplicitParam(name = "id",value = "当前小程序码的id",dataType = "int",paramType = "path",required = true)
    @GetMapping("/findAllGoodsInQr/{id}")
    public AjaxResult findAllGoodsInQr(@PathVariable Long id) {
        return AjaxResult.success(wxGoodsService.findAllGoodsInQr(id));
    }

    /***
     * 更新商品
     * @param wxGoods 商品类
     * @return 影响行数
     */
    @ApiOperation("更新商品")
    @ApiImplicitParam(name = "wxGoods",value = "商品对应的实体类",dataType = "WxGoods",required = true,paramType = "form")
    @ApiResponses({
            @ApiResponse(code = 200,message = "更新商品成功"),
            @ApiResponse(code = 500,message = "更新商品失败"),
    })
    @RequestMapping(value = "/updateWxGoods",method = RequestMethod.PUT)
    public AjaxResult updateWxGoods(@RequestBody WxGoods wxGoods) {
        int affectNum = wxGoodsService.updateWxGoods(wxGoods);
        return toAjax(affectNum);
    }

    /***
     * 删除商品
     * @param id 商品id
     * @return
     */
    @ApiOperation("删除商品")
    @ApiImplicitParam(name = "id",value = "商品id",required = true,paramType = "path")
    @ApiResponses({
            @ApiResponse(code = 200,message = "删除商品成功"),
            @ApiResponse(code = 500,message = "删除商品失败"),
    })
    @RequestMapping(value = "/deleteWxGoodsById/{id}",method = RequestMethod.DELETE)
    public AjaxResult deleteWxGoodsById(@PathVariable Long id) {
        int affectNum = wxGoodsService.deleteWxGoodsById(id);
        return toAjax(affectNum);
    }

    /**
     * 导出【商品】列表
     */
    @ApiOperation("导出商品列表")
    @Log(title = "【商品列表】", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(WxGoods wxGoods)
    {
        List<WxGoods> list = wxGoodsService.selectWxGoodsList(wxGoods);
        ExcelUtil<WxGoods> util = new ExcelUtil<>(WxGoods.class);
        return util.exportExcel(list, "attr");
    }
}
